package com.android.act;

import com.android.R;
import com.android.R.id;
import com.android.R.layout;
import com.android.R.string;
import com.android.TabMainActivity;
import com.android.common.Action;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

/**
 * 登录页面
 * 
 * @author Sunlibin
 * 
 */

public class LoginActivity extends Activity implements OnClickListener {

	Action action;

	private EditText edt_userId;
	private EditText edt_passWord;
	private Button btn_login;
	private Button btn_forgetpsw;

	private static String str_UserId;
	private static String str_PassWord;

	static String oauth_token;
	static String oauth_token_secret;
	static String uid;

	private final int LOGIN_TAG_SUCESS = 0;
	private final int LOGIN_TAG_FAILURE = 1;

	Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {

			Bundle b = msg.getData();
			String result_Msg = b.getString("login_Msg");
//			action.showShortToast(result_Msg);
			action.showShortToast("登录成功");
			switch (msg.what) {

			case LOGIN_TAG_SUCESS:
				action.stopProgressDialog();
				Intent intent = new Intent(LoginActivity.this, TabMainActivity.class);
				// intent.putExtras(b);
				startActivity(intent);
				finish();
				break;
			case LOGIN_TAG_FAILURE:
				action.stopProgressDialog();
				break;
			}
			super.handleMessage(msg);
		}
	};

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activtiy_login_view);
		action = new Action(LoginActivity.this);
		findView();
	}

	private void findView() {
		edt_userId = (EditText) LoginActivity.this
				.findViewById(R.id.et_account);
		edt_passWord = (EditText) LoginActivity.this.findViewById(R.id.et_psw);
		btn_login = (Button) LoginActivity.this.findViewById(R.id.btn_signin);
		btn_login.setOnClickListener(this);
		btn_forgetpsw = (Button) LoginActivity.this
				.findViewById(R.id.btn_forget_pwd);
		btn_forgetpsw.setOnClickListener(this);
	}

	public void doLogin(final String userId, final String passWord) {
		new Thread() {
			@Override
			public void run() {

				Message msg = new Message();

				msg.what = LOGIN_TAG_SUCESS;
				//
				// Bundle bundle = new Bundle();
				// bundle.putString("login_Msg", login_Msg);
				// bundle.putString("oauth_token", oauth_token);
				// bundle.putString("oauth_token_secret", oauth_token_secret);
				// bundle.putString("uid", uid);
				// msg.setData(bundle);
				handler.sendMessageDelayed(msg, 2000);

			}
		}.start();

	}

	private void prepareLogin() {
		str_UserId = edt_userId.getText().toString().trim();
		str_PassWord = edt_passWord.getText().toString().trim();

		if (str_UserId == null || str_UserId.length() == 0) {
			action.showShortToast(getResources().getString(
					R.string.login_no_userId));
		} else if (str_PassWord == null || str_PassWord.length() == 0) {
			action.showShortToast(getResources().getString(
					R.string.login_no_userPassword));
		} else {
			// action.showProgressDialog(getResources().getString(
			// R.string.login_hint_progressing));
			action.startProgressDialog("正在登录");
			doLogin(str_UserId, str_PassWord);
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_signin:

			prepareLogin();

			break;

		case R.id.btn_forget_pwd:

			// Intent intent = new Intent().setClass(this,
			// UserInfoActivity.class);
			// startActivity(intent);

			break;

		default:
			break;
		}

	}

}